import os
import sys
import time

INFO = 'info'
WARNING = 'warning'
ERROR = 'error'
MESSAGE = 'message'
logfile = open('log.txt','a+')
logfile.write("\n"+'---'+"\n")
if os.name == "nt": #windows supports the codes directly..
	import ctypes
	STD_INPUT_HANDLE = -10
	STD_OUTPUT_HANDLE = -11
	STD_ERROR_HANDLE = -12
	
	SetConsoleTextAttribute = ctypes.windll.kernel32.SetConsoleTextAttribute
	GetConsoleScreenBufferInfo = ctypes.windll.kernel32.GetConsoleScreenBufferInfo
	def set_colour(colour):
		std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
		colour = int(colour,16) #mc codes match windows... (coincidence?)
		bool = SetConsoleTextAttribute(std_out_handle, colour)
		#print "err: %s" % (ctypes.windll.kernel32.GetLastError(),)
		return bool
else: #ansi
	ctrl = chr(27)+"["
	cols = {
		"0":"0;30;2",
		"1":"0;34;2",
		"2":"0;32;2",
		"3":"0;36;2",
		"4":"0;31;2",
		"5":"0;35;2",
		"6":"0;33;2",
		"7":"0;37;2",
		#same as before, just bright :)
		"8":"0;30;1",
		"9":"0;34;1",
		"a":"0;32;1",
		"b":"0;36;1",
		"c":"0;31;1",
		"d":"0;35;1",
		"e":"0;33;1",
		"f":"0;37;1",
	}
	def set_colour(colour):
		col = cols[colour]
		sys.stdout.write(ctrl+col+"m")

def log(msg,type=False):
	if type:
		if type==WARNING:
			msg = "&eWarning: "+msg
		elif type==ERROR:
			msg = "&cERROR: "+msg
		elif type==INFO:
			msg = "&9"+msg
		elif type==MESSAGE:
			msg = "&3"+msg
			
	msg = "&f("+time.strftime("%m/%d/%Y %H:%M:%S")+") &7"+msg+"&7"
	#print msg
	split = msg.split("&")
	for section in split:
		if section != "":
			col = section[0]
			if "0123456789abcdef".find(col) == -1:
				col = "f"
			set_colour(col)
			msg = section[1:]
			sys.stdout.write(msg)
			logfile.write(msg)
	sys.stdout.write("\n")
	logfile.write("\n")